package com.lgx.mall.core.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.dao.IImageDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.core.dao.IAdvertisementDao;
import com.lgx.mall.core.entity.Advertisement;

@Component("advertisementDao")
public class AdvertisementDao extends GeneralDao  implements IAdvertisementDao {
	
	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private IImageDao imageDao;

	public IImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(IImageDao imageDao) {
		this.imageDao = imageDao;
	}
	
	@Transactional
	public Advertisement createAdvertisement(Advertisement advertisement) throws DataException {
		this.getHibernateTemplate().save(advertisement);
		log.info("[创建广告]:"+advertisement.getId());
		return advertisement;
	}

	@Transactional
	public Advertisement updateAdvertisement(Advertisement advertisement) throws DataException {
		this.getHibernateTemplate().update(advertisement);
		log.info("[更新广告]:"+advertisement.getId());
		return advertisement;
	}

	@Transactional
	public void deleteAdvertisement(Advertisement advertisement) throws DataException {
		log.info("[删除广告]:"+advertisement.getId());
		if(!StringUtils.isEmpty(advertisement.getImageUrl()))this.imageDao.deleteImageByFilePath(advertisement.getImageUrl());
		this.getHibernateTemplate().delete(advertisement);
	}

	public void deleteAdvertisement(long id) throws DataException {
		this.deleteAdvertisement(this.findAdvertisementById(id));
		log.info("[删除广告]:"+id);
	}

	
	public Advertisement findAdvertisementById(long id) {
		return this.getHibernateTemplate().get(Advertisement.class, id);
	}

	public void deleteAdvertisements(String[] ids) throws DataException {
		for(String id : ids)
		{
			this.deleteAdvertisement(Long.parseLong(id));
		}
	}

	@SuppressWarnings("unchecked")
	public List<Advertisement> findAdvertisementList() {
		String hql = "from Advertisement a order by a.id asc";
		return this.getHibernateTemplate().find(hql);
	}

	public Pagination findAdvertisementPagination(Object[] params,
			int pageSize, int pageNum) {
		String sql = "from Advertisement a ";
		
		if(Integer.parseInt(params[0].toString()) == 2){
			sql += " where a.merchant.id = " + params[1];
		}
		
		sql += " order by  a.layerOrder desc,a.id asc";
		
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

	@Override
	public Pagination findAdvertisementPagination(String areaNode,
			String county, int pageSize, int pageNum) {
		
		String sql = "from Advertisement a where 1=1";
		
		if(!StringUtils.isEmpty(areaNode)){
			sql += " and a.merchant.areaNode = '" + areaNode + "'";
		}
		
		if(!StringUtils.isEmpty(county)){
			sql += " and a.merchant.county = '" + county + "'";
		}
		
		sql += " order by  a.layerOrder desc,a.id asc";
		
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

}
